import { cloneDeep } from 'lodash-es'
import { ref, unref } from 'vue'

export const useState = initialValue => {
    let cache

    refreshCache(initialValue)

    const state = ref(initialValue)

    const setState = val => {
        state.value = val
    }

    const resetState = () => {
        state.value = cloneDeep(cache)
    }

    function refreshCache (data) {
        cache = cloneDeep(data)
    }

    return {
        state,
        setState,
        resetState,
        refreshCache
    }
}